﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;

using EyesOfTheDragon.Events;

using Anthem.GameScreens;

using Anthem.Components;
using EyesOfTheDragon.Globals;

using Microsoft.Xna.Framework.Graphics;
using XRpgLibrary.SpriteClasses;
using XRpgLibrary.Events;

namespace EyesOfTheDragon.Components
{
    public class ThreeBabyHole:IPauseable
    {
        public void Update(GameTime gt)
        {
            if (_random == null)
                _random = new Random();
            float elapsedSeconds = gt.ElapsedGameTime.Milliseconds / 1000f;
            lastBaby -= elapsedSeconds;
            if (lastBaby < 0 && numBaby < maxBaby)
            {
                makeBaby();
                lastBaby = babyFrequency;
                numBaby++;
            }

            if (Vector2.Distance(Player.Instance.Sprite.position, HoleTile.Position.toVector())> 250)
                Active = false;
            
        }
        public void makeBaby()
        {

            AnimatedSprite sprite1;
            Texture2D spriteSheet2 = GamePlayScreen.Instance.GameRef.Content.Load<Texture2D>(@"MonsterSprites\child_Running");
            Texture2D monsterAttack = GamePlayScreen.Instance.GameRef.Content.Load<Texture2D>(@"MonsterSprites\Child_attack");
            Dictionary<AnimationKey, Animation> animations1 = new Dictionary<AnimationKey, Animation>();
            Animation animation = new Animation(4, 53, 106, 0, 106);
            animation.Texture = spriteSheet2;
            animations1.Add(AnimationKey.Down, animation);
            animation = new Animation(4, 53, 106, 0, 106);
            animation.Texture = spriteSheet2;
            animations1.Add(AnimationKey.Left, animation);
            animation = new Animation(4, 53, 106, 0, 212);
            animation.Texture = spriteSheet2;
            animations1.Add(AnimationKey.Right, animation);
            animation = new Animation(3, 53, 106, 0, 212);
            animation.Texture = spriteSheet2;
            animations1.Add(AnimationKey.Up, animation);

            animation = new Animation(4, 53, 106, 0, 106);
            animation.Texture = monsterAttack;
            animations1.Add(AnimationKey.AttackLeft, animation);

            animation = new Animation(4, 53, 106, 0, 212);
            animation.Texture = monsterAttack;
            animations1.Add(AnimationKey.AttackRight, animation);


            sprite1 = new AnimatedSprite(spriteSheet2, animations1);
            BaseMonster bm = new BaseMonster(sprite1);
            bm.init();
            sprite1.position = HoleTile.Position.toVector();
            sprite1.position -= new Vector2(0, HoleTile.RectPosition.Height);
            bm.OurMonsterType = PauseManager.PauseType.Child;
            Monster.Instance.Monsters.Add(bm);
        }
        public GTile TriggerTile;
        public GTile CrackTile;
        public GTile HoleTile;
        public bool Active = true;
        public int maxBaby = 6;
        public int numBaby = 0;
        private float lastBaby = 0;
        private float babyFrequency = 2;
        private Random _random;
    }
    public class HoleTraps :IUpdate
    {
        private const string triggerLayer = "triggersLayer";
        private const string holeLayer = "HoleLayer";
        private const string pitLayer = "PitLayer";
        private const float crackDist = 400;
        private const float holeDist = 128;
        private ThreeBabyHole _currHole;

        public bool Enabled
        {
            get
            {
                return true;
            }
            set { }
        }
        public static HoleTraps Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new HoleTraps();
                return _instance;
            }
        }

        public void Update(GameTime gt)
        {
            List<GTile> tiles = map.Instance.getLayer(triggerLayer).getTiles(MapVariables.HoleTrapsGID);
            foreach( GTile t in tiles)
            {
                GTile _crackTile = map.Instance.getLayer(pitLayer).getTileByVector(t.Position.toVector());
                GTile _pitTile = map.Instance.getLayer(holeLayer).getTileByVector(t.Position.toVector());
                if (Vector2.Distance(Player.Instance.Sprite.position, t.Position.toVector()) < holeDist)
                {

                    _pitTile.Visible = true;

                    _crackTile.Visible = false;
                    if (_currHole == null)
                    {
                        _currHole = new ThreeBabyHole();
                        _currHole.CrackTile = _crackTile;
                        _currHole.HoleTile = _pitTile;
                        _currHole.TriggerTile = t;
                        break;
                    }
                }
                else if (Vector2.Distance(Player.Instance.Sprite.position, t.Position.toVector()) < crackDist)
                {
                    _crackTile.Visible = true;
                    _pitTile.Visible = false;
                }
                else
                {
                     _crackTile.Visible = false;
                     _pitTile.Visible = false;
                    if (_currHole != null && !_currHole.Active)
                        _currHole = null;
                }
                
               
                
            }
            if (_currHole != null)
                _currHole.Update(gt);
            
            
        }
        private static HoleTraps _instance;
    }
}
